const { executeQuery } = require('./mongo_util')

//用变量接受用户表名
const collectionName = "User";


//登录验证
function login(user, onRetGot, onErrGot) {
    console.log("shuju",user);
    
    executeQuery(collectionName, (collection, callback) => {
        collection.findOne(user).then((res) => {
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

//注册
function register(user, onRetGot, onErrGot) {
    // let data = new Date()
    // let id = data.getTime();
    // console.log("user",user);
    // user.id = id;
    executeQuery(collectionName, (collection, callback) => {
        collection
            .insertOne(user)
            .then((result) => {
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
    
}


//修改
function update(user, onRetGot, onErrGot) {
    let id = user.id 
    let obj = {...user};
    // user.username ? obj.username = user.username : '';
    // user.password ? obj.password = user.password : '';
    console.log(obj);
    executeQuery(collectionName, (collection, callback) => {
        collection
            .update({"id":id},{$set:obj})
            .then((result) => {
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}


//查询
function select(user, onRetGot, onErrGot) {
    id = user.id;
    console.log("123",id);
    executeQuery(collectionName, (collection, callback) => {
        collection
            .find({"id":id}).toArray()
            .then((result) => {
                console.log(result);
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}
//删除
function deleteuser(user, onRetGot, onErrGot) {
    let ids = user.id
    console.log(user);
    console.log(ids);
    executeQuery(collectionName, (collection, callback) => {
        collection
            .deleteMany({"id":{$in:ids}})
            .then((result) => {
                onRetGot(result);
                callback();
            })
            .catch((err) => {
                onErrGot(err);
                callback();
            });
    });
}


//查询
// function all(user, onRetGot, onErrGot) {
//     executeQuery(collectionName, (collection, callback) => {
//         collection.find({}).count().then(async(res) => {
//             let obj = {};
//             obj.total = res;
//             await collection.find({}).toArray().then((bbq => {
//                 obj.data = bbq;
//             }))
//             onRetGot(obj);
//             callback()
//         })
//             .catch((err) => {
//                 onRetGot(err);
//                 callback();
//         })
//     })
// }

function all(goods, onRetGot, onErrGot) { 

    executeQuery(collectionName, (collection, callback) => {
        collection.find({}).count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find({}).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}









module.exports = {
    login,
    register,
    update,
    select,
    all,
    deleteuser,
}